package cse.main;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Algorithm extends JFrame {

	private final String INSERTION = "Insertion Sort";
	private final String MERGE = "Merge Sort";
	private final String QUICK = "Quick Sort";
	private final String QUICK_INSERTION = "Quick Insertion Sort";
	private int size;
	private String algorithm;
	private int orderAlgorithm;
	private int[] array;
	private JPanel contentPane;
	private JTextField textThrehold;
	private JComboBox cbAlgorithm;
	private JLabel lblThrehold;
	private JComboBox cbSize;
	private JLabel lbSize, lbInsertion, lbMerge, lbQuick, lbQuickInsertion;

	public Algorithm() {
		initComponent();
		contructor();
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Algorithm frame = new Algorithm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */

	private void initComponent() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblSetSize = new JLabel("Set Size");
		lblSetSize.setBounds(85, 11, 62, 14);
		contentPane.add(lblSetSize);

		cbSize = new JComboBox();
		cbSize.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				size = Integer.parseInt((String) cbSize.getSelectedItem());
				create(size);
				showSize();
				resetResut(lbInsertion, INSERTION);
				resetResut(lbMerge, MERGE);
				resetResut(lbQuick, QUICK);
				resetResut(lbQuickInsertion, QUICK_INSERTION);
			}
		});
		cbSize.setModel(new DefaultComboBoxModel(new String[] { "10", "50",
				"100", "500", "1000", "5000", "10000", "50000", "100000",
				"500000", "1000000", "5000000", "10000000", "15000000",
				"20000000" }));
		cbSize.setBounds(182, 8, 126, 20);
		contentPane.add(cbSize);

		JLabel lblAlgorithm = new JLabel("Algorithm");
		lblAlgorithm.setBounds(85, 41, 62, 14);
		contentPane.add(lblAlgorithm);

		cbAlgorithm = new JComboBox();
		cbAlgorithm.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				algorithm = (String) cbAlgorithm.getSelectedItem();
				orderAlgorithm = cbAlgorithm.getSelectedIndex();
				if (algorithm.equals("Quick Insertion Sort")) {
					lblThrehold.setEnabled(true);
					textThrehold.setEnabled(true);
				} else {
					lblThrehold.setEnabled(false);
					textThrehold.setEnabled(false);
				}
			}
		});
		cbAlgorithm.setModel(new DefaultComboBoxModel(new String[] {
				"Insertion Sort", "Merge Sort", "Quick Sort",
				"Quick Insertion Sort" }));
		cbAlgorithm.setBounds(182, 38, 126, 20);
		contentPane.add(cbAlgorithm);

		lblThrehold = new JLabel("Threhold");
		lblThrehold.setEnabled(false);
		lblThrehold.setBounds(85, 73, 62, 14);
		contentPane.add(lblThrehold);

		textThrehold = new JTextField();
		textThrehold.setEnabled(false);
		textThrehold.setBounds(182, 69, 126, 20);
		contentPane.add(textThrehold);
		textThrehold.setColumns(10);

		JButton btnExecute = new JButton("Execute");
		btnExecute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					SortingAlgorithms sortingAlgorithms = new SortingAlgorithms(
							array);
					switch (orderAlgorithm) {
					case 0:
						showResult(lbInsertion, algorithm,
								sortingAlgorithms.insertionSort());
						break;
					case 1:
						showResult(lbMerge, algorithm,
								sortingAlgorithms.mergeSort());
						break;
					case 2:
						showResult(lbQuick, algorithm,
								sortingAlgorithms.quickSort());
						break;
					case 3:
						if (textThrehold.getText().equals("")) {
							JOptionPane.showMessageDialog(null,
									"Please insert threhold");
						} else {
							sortingAlgorithms.setThrehold(Integer
									.parseInt(textThrehold.getText()));
							showResult(lbQuickInsertion, algorithm,
									sortingAlgorithms.quickInsertionSort());
						}
						break;
					}
				} catch (Exception e) {
					// TODO: handle exception
					JOptionPane.showMessageDialog(null, "Error");
				}
			}
		});
		btnExecute.setBounds(221, 100, 89, 23);
		contentPane.add(btnExecute);

		lbSize = new JLabel("This size of the set is 10");
		lbSize.setBounds(49, 134, 314, 14);
		contentPane.add(lbSize);

		lbInsertion = new JLabel(
				"The time of the Insertion Sort Algorithm is 0ms");
		lbInsertion.setBounds(49, 159, 314, 14);
		contentPane.add(lbInsertion);

		lbMerge = new JLabel("The time of the Merge Sort Algorithm is 0ms");
		lbMerge.setBounds(49, 185, 314, 14);
		contentPane.add(lbMerge);

		lbQuick = new JLabel("The time of the Quick Sort Algorithm is 0ms ");
		lbQuick.setBounds(49, 210, 314, 14);
		contentPane.add(lbQuick);

		lbQuickInsertion = new JLabel(
				"The time of the Quick Insertion Sort Algorithm is 0ms");
		lbQuickInsertion.setBounds(49, 235, 314, 14);
		contentPane.add(lbQuickInsertion);
	}

	private void showResult(JLabel label, String algorithm, String duration) {
		label.setText("The time of the " + algorithm + " is " + duration);
	}

	private void resetResut(JLabel label, String algorithm) {
		// TODO Auto-generated method stub
		label.setText("The time of the " + algorithm + " is 0ms");
	}

	private void showSize() {
		// TODO Auto-generated method stub
		lbSize.setText("This size of the set is " + size);
	}

	private void create(int size) {
		// TODO Auto-generated method stub
		array = new int[size];
		for (int i = 0; i < array.length; i++) {
			array[i] = (int) (Math.random() * array.length * array.length);
		}
	}

	private void contructor() {
		// TODO Auto-generated method stub
		size = 10;
		algorithm = INSERTION;
		orderAlgorithm = 0;
		create(size);

	}
}
